<template>
    <el-dialog v-model="props.visible" title="编辑福利产品" width="25%" @close="handleClose">
        <el-form :model="formData" ref="formRef">
            <el-form-item label="产品名称" prop="welfareName">
                <el-input v-model="formData.welfareName" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="适配星级" prop="level">
                <el-row>
                    <el-col :span="6">
                        <el-select v-model="formData.lowLevel">
                            <el-option v-for="item in levelOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-col>
                    <el-col :span="1">
                        <span>至</span>
                    </el-col>
                    <el-col :span="6">
                        <el-select v-model="formData.hightLevel">
                            <el-option v-for="item in levelOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-col>
                </el-row>
            </el-form-item>
            <el-form-item label="激活状态" prop="productStatus">
                <el-select v-model="formData.productStatus" placeholder="请选择">
                    <el-option label="已激活" :value="'1'"></el-option>
                    <el-option label="已禁用" :value="'0'"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上传图片" prop="image">
                <el-upload class="upload-demo" 
                    v-model:file-list="fileList"
                    drag 
                    ref="uploadRef"
                    single
                    :action="uploadImgUrl"
                    :headers="headers"
                    :before-upload="beforeAvatarUpload" 
                    :on-success="handleAvatarSuccess"
                >
                    <el-icon class="el-icon--upload"><Plus /></el-icon>
                    <div class="el-upload__text">点击或拖拽文件到此处上传</div>
                    <template #tip>
                        <div class="el-upload__tip">
                            文件不超过10MB
                        </div>
                    </template>
                </el-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button  type="primary" :disabled="shouldSubmit" @click="handleSubmitEdit" >确 定</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance,UploadInstance, UploadProps,UploadUserFile } from 'element-plus';
import { Plus } from '@element-plus/icons-vue';
import { globalHeaders } from "@/utils/request";
import { updateWelfareApi } from '@/api/sj/welfare-crm/index';

const props = defineProps(['visible','data']);
const emits = defineEmits(['close','submit']);
const formRef = ref<FormInstance>();
const uploadRef = ref<UploadInstance>();
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const uploadImgUrl = ref(baseUrl + "/resource/oss/upload");
const headers = ref(globalHeaders);
const fileList = ref<UploadUserFile[]>([]);
const shouldSubmit = ref(false);
const levelOption = [
    {label:'无',value:'0'},
    {label:'一星',value:'1'},
    {label:'二星',value:'2'},
    {label:'三星',value:'3'},
    {label:'四星',value:'4'},
    {label:'五星',value:'5'},
];
const data = reactive({
    formData:{
        welfareName: undefined,
        lowLevel: undefined,
        hightLevel: undefined,
        productStatus: undefined,
        showImage: undefined,
        initialQuantity: undefined,
        type: 2,
        purchaseFlag: 0
    }
});
const { formData } = toRefs(data);

watch(()=>props.data,(newData)=>{
    // Object.assign(formData.value, newData);
    formData.value = {...newData};
    fileList.value.push({name: 'image',url:newData.showImage});
},{deep:true});

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.size / 1024 / 1024 > 10) {
        ElMessage.error('图片大小不可超过 10MB!')
        return false;
    }
    return true;
};

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response
    ) => {
        shouldSubmit.value = false;
        formData.value.showImage = response.data.url;
        fileList.value[0] = response.data;
        shouldSubmit.value = true;
};

const handleSubmitEdit = async ()=>{
    const result = await updateWelfareApi(formData.value);
    if(result.code == 200){
        emits('submit',true);
        handleClose();
    }else{
        emits('submit',false);
    };
};

const handleClose = ()=>{
    formRef.value?.resetFields();
    formData.value.hightLevel = undefined;
    formData.value.lowLevel = undefined;
    uploadRef.value?.clearFiles();
    fileList.value = [];
    shouldSubmit.value = false;
    emits('close');
};

</script>

<style scoped>

</style>